#include <iostream>
using namespace std;

double getAverage(int arr[], int size);
int *getRandom();

int main()
{
    // 带有 5 个元素的双精度浮点型数组
    double runoobAarray[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
    double *p;

    p = runoobAarray;

    // 输出数组中每个元素的值
    cout << "使用指针的数组值 " << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << "*(p + " << i << ") : ";
        cout << *(p + i) << endl;
    }

    cout << "使用 runoobAarray 作为地址的数组值 " << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << "*(runoobAarray + " << i << ") : ";
        cout << *(runoobAarray + i) << endl;
    }

    cout << "带有 5 个元素的整型数组" << endl;
    // 带有 5 个元素的整型数组
    int balance[5] = {1000, 2, 3, 17, 50};
    double avg;

    // 传递一个指向数组的指针作为参数
    avg = getAverage(balance, 5);

    // 输出返回值
    cout << "平均值是：" << avg << endl;

    cout << "C++ 从函数返回数组" << avg << endl;

    // 一个指向整数的指针
    int *q;

    q = getRandom();

    for (int i = 0; i < 10; i++)
    {
        cout << "*(q + " << i << ") : ";
        cout << *(q + i) << endl;
    }

    return 0;
}

double getAverage(int arr[], int size)
{
    int i, sum = 0;
    double avg;

    for (i = 0; i < size; ++i)
    {
        sum += arr[i];
    }

    avg = double(sum) / size;

    return avg;
}

// 要生成和返回随机数的函数
int *getRandom()
{
    static int r[10];

    // 设置种子
    srand((unsigned)time(NULL));
    for (int i = 0; i < 10; ++i)
    {
        r[i] = rand();
        cout << r[i] << endl;
    }

    return r;
}